#include <fstream>
#include <string>
#include <filesystem>
#include <algorithm>
#include <cstring>
#include "folder.h"
#include "page.h"
using namespace std;
using namespace filesystem;
bool contains(vector<string> v, string s){
	return (v.size() > 0) and (find(v.begin(), v.end(), s) != v.end());
}

folder::~folder(){}
folder::folder(){}

folder::folder(string path){
	this->path = path;
	for(const auto& entry : directory_iterator(path)){
		string path = entry.path();
	 
		if(is_directory(entry.path()))
			folder(entry.path()).convert();
			
		else{
			if(path[path.size() - 7] == '/'){
				string check_string = path.substr(path.size() - 6);
				cout << check_string << endl;
				if(check_string == "header"){
					_header_path = path;
				}
			}else if(path[path.size() - 4] == '/'){
				string check_string = path.substr(path.size() - 3);
				cout << check_string << endl;

				if(check_string == "end"){
					_end_path = path;
				}
				
			}else if(path[path.size() - 7] == '.'){
				string check_string = path.substr(path.size() - 6);
				//cout << check_string << endl;
				cout << path << endl;
				if(check_string == "matedi"){
					_dot_matedi_path = path;
				}
			}
		}
	}
}
void folder::get_dot_matedi_config(string dot_matedi_path){
	ifstream in(dot_matedi_path);
	string line;
	while(getline(in,line)){

		size_t aux = line.find("header: ");
		if(aux != string::npos){
			line = line.substr(aux + 8);
			_header_path = line;
		}

		aux = line.find("end: ");
		if(aux != string::npos){
			line = line.substr(aux + 5);
			_end_path = line;
		}

		char * pch;
		aux = line.find("header_only: ");
		if(aux != string::npos){
			line = line.substr(aux + 13);
			const char * str = line.c_str();
			pch = strtok((char*)str, ", ");
			while(pch != NULL){
				_header_only.push_back(string(pch));
				pch = strtok (NULL, ", ");
			}
		}

		aux = line.find("end_only: ");
		if(aux != string::npos){
			line = line.substr(aux + 10);
			const char * str = line.c_str();
			pch = strtok((char *)str, ", ");
			while(pch != NULL){
				_end_only.push_back(string(pch));
				pch = strtok (NULL, ", ");
			}
		}

		aux = line.find("clear: ");
		if(aux != string::npos){
			line = line.substr(aux + 7);
			const char * str = line.c_str();
			pch = strtok((char *)str, ", ");
			while(pch != NULL){
				_clear.push_back(string(pch));
				pch = strtok (NULL, ", ");
			}
		}
	}
}

void folder::convert(){
	string header;
	string end;
	if(!_dot_matedi_path.empty()){
		get_dot_matedi_config(_dot_matedi_path);
	}
	string current_folder = current_path();
	if(!_header_path.empty() && !_end_path.empty()){
		header = load_file(_header_path);
		end = load_file(_end_path);
		for(const auto& entry : directory_iterator(path)){
			
			if(!is_directory(entry)){
				string aux_path = entry.path();
				string folder_and_name = aux_path.substr(current_folder.size() + 1);
				string filename = aux_path.substr(aux_path.size() - 6);
				if((filename.find("matedi") == string::npos) && 
					 (filename.find("header") == string::npos) &&
					 (filename.find("end") == string::npos)){
					if(contains(_header_only, filename)){
						
						page p(aux_path, header, "");
						p.save();
					}else if(contains(_end_only, filename)){
						end = load_file(_end_path);
						page p(aux_path, "", end);
						p.save();
					}else if(contains(_clear, filename)){
						page p(aux_path, "", "");
						p.save();
					}else{
						page p(aux_path, header, end);
						p.save();
					}
				}
			}
		}
	}
}
string folder::load_file(string path){
	ifstream reader(path);
	string line;
	string result;
	while(getline(reader,line))
		result += line + "\n";
	reader.close();
	return result;
}
folder& folder::operator =(const folder& F){
	if(this != &F)  // avoid self-allocation
		path = F.path;
	
	return *this;
}
